//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "DiscretizedSwaption.h"
using namespace Cephei::QL::Pricingengines::Swaption;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/DiscretizedAsset.h>
#include <gen/QL/Lattice.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/DiscretizedOption.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Swaption::CDiscretizedSwaption::CDiscretizedSwaption (boost::shared_ptr<QuantLib::DiscretizedSwaption>& childNative, Object^ owner) : CDiscretizedOption(CDiscretizedSwaption::typeid)
{
#ifdef HANDLE
	_phDiscretizedSwaption = NULL;
#endif
	_ppDiscretizedSwaption = &childNative;
    _ppDiscretizedOption = new boost::shared_ptr<QuantLib::DiscretizedOption> (boost::dynamic_pointer_cast<QuantLib::DiscretizedOption> (*_ppDiscretizedSwaption));
}
Cephei::QL::Pricingengines::Swaption::CDiscretizedSwaption::CDiscretizedSwaption (QuantLib::DiscretizedSwaption& childNative, Object^ owner) : CDiscretizedOption(CDiscretizedSwaption::typeid)
{
#ifdef HANDLE
	_phDiscretizedSwaption = NULL;
#endif
	_ppDiscretizedSwaption = new boost::shared_ptr<QuantLib::DiscretizedSwaption> (&childNative);
    _ppDiscretizedOption = new boost::shared_ptr<QuantLib::DiscretizedOption> (boost::dynamic_pointer_cast<QuantLib::DiscretizedOption> (*_ppDiscretizedSwaption));
    _DiscretizedSwaptionOwner = owner;
    _DiscretizedOptionOwner = owner;
}

Cephei::QL::Pricingengines::Swaption::CDiscretizedSwaption::CDiscretizedSwaption (CDiscretizedSwaption^ copy) : CDiscretizedOption(CDiscretizedSwaption::typeid)
{
#ifdef HANDLE
	_phDiscretizedSwaption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDiscretizedSwaption = new boost::shared_ptr<QuantLib::DiscretizedSwaption> (copy->GetShared());
        _ppDiscretizedOption = new boost::shared_ptr<QuantLib::DiscretizedOption> (boost::dynamic_pointer_cast<QuantLib::DiscretizedOption> (*_ppDiscretizedSwaption));
    }
}
Cephei::QL::Pricingengines::Swaption::CDiscretizedSwaption::CDiscretizedSwaption (PLATFORM::Type^ t) : CDiscretizedOption(CDiscretizedSwaption::typeid)
{
#ifdef HANDLE
	_phDiscretizedSwaption = NULL;
#endif
	if (!t->IsSubclassOf(CDiscretizedSwaption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Swaption::CDiscretizedSwaption::CDiscretizedSwaption (QuantLib::Handle<QuantLib::DiscretizedSwaption>& childNative, Object^ owner)  : CDiscretizedOption(CDiscretizedSwaption::typeid)
{
	_phDiscretizedSwaption = &childNative;
	_ppDiscretizedSwaption = &static_cast<boost::shared_ptr<QuantLib::DiscretizedSwaption>>(childNative.currentLink());
    _ppDiscretizedOption = new boost::shared_ptr<QuantLib::DiscretizedOption> (boost::dynamic_pointer_cast<QuantLib::DiscretizedOption> (*_ppDiscretizedSwaption));
    _DiscretizedSwaptionOwner = owner;
}
Cephei::QL::Pricingengines::Swaption::CDiscretizedSwaption::CDiscretizedSwaption (QuantLib::Handle<QuantLib::DiscretizedSwaption> childNative)  : CDiscretizedOption(CDiscretizedSwaption::typeid)
{
	_phDiscretizedSwaption = &childNative;
	_ppDiscretizedSwaption = &static_cast<boost::shared_ptr<QuantLib::DiscretizedSwaption>>(childNative.currentLink());
    _ppDiscretizedOption = new boost::shared_ptr<QuantLib::DiscretizedOption> (boost::dynamic_pointer_cast<QuantLib::DiscretizedOption> (*_ppDiscretizedSwaption));
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Swaption::CDiscretizedSwaption::CDiscretizedSwaption (QuantLib::DiscretizedSwaption childNative)  : CDiscretizedOption(CDiscretizedSwaption::typeid)
{
#ifdef HANDLE
	_phDiscretizedSwaption = NULL;
#endif
	_ppDiscretizedSwaption = new boost::shared_ptr<QuantLib::DiscretizedSwaption> (new QuantLib::DiscretizedSwaption (childNative));
    _ppDiscretizedOption = new boost::shared_ptr<QuantLib::DiscretizedOption> (boost::dynamic_pointer_cast<QuantLib::DiscretizedOption> (*_ppDiscretizedSwaption));
}
#endif

Cephei::QL::Pricingengines::Swaption::CDiscretizedSwaption::~CDiscretizedSwaption ()
{
    if (_ppDiscretizedSwaption != NULL)
    {
	    delete _ppDiscretizedSwaption;
        _ppDiscretizedSwaption = NULL;
    }
}
Cephei::QL::Pricingengines::Swaption::CDiscretizedSwaption::!CDiscretizedSwaption ()
{
    if (_ppDiscretizedSwaption != NULL)
    {
	    delete _ppDiscretizedSwaption;
    }
}
QuantLib::DiscretizedSwaption& Cephei::QL::Pricingengines::Swaption::CDiscretizedSwaption::GetReference ()
{
    if (_ppDiscretizedSwaption == NULL) throw REFNEW NativeNullException ();
	return **_ppDiscretizedSwaption;
}
boost::shared_ptr<QuantLib::DiscretizedSwaption>& Cephei::QL::Pricingengines::Swaption::CDiscretizedSwaption::GetShared ()
{
    if (_ppDiscretizedSwaption == NULL) throw REFNEW NativeNullException ();
	return *_ppDiscretizedSwaption;
}
QuantLib::DiscretizedSwaption* Cephei::QL::Pricingengines::Swaption::CDiscretizedSwaption::GetPointer ()
{
    if (_ppDiscretizedSwaption == NULL) throw REFNEW NativeNullException ();
	return &**_ppDiscretizedSwaption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DiscretizedSwaption>& Cephei::QL::Pricingengines::Swaption::CDiscretizedSwaption::GetHandle ()
{
	if (_phDiscretizedSwaption == NULL)
	{
		_phDiscretizedSwaption = new Handle<QuantLib::DiscretizedSwaption> (*_ppDiscretizedSwaption);
	}
	return *_phDiscretizedSwaption;
}
#endif
bool Cephei::QL::Pricingengines::Swaption::CDiscretizedSwaption::HasNative () 
{
	return (_ppDiscretizedSwaption != NULL);
}

Cephei::QL::Pricingengines::Swaption::IDiscretizedSwaption^ Cephei::QL::Pricingengines::Swaption::CDiscretizedSwaption::Reset (UInt64 size)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _size = (QuantLib::Size)ValueHelper::Convert (size); //a
    	(*_ppDiscretizedSwaption)->reset ( _size );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

